package cc.zzone.controller;

import cc.zzone.bean.ChatWebsocketServer;
import cc.zzone.bean.Result;
import cc.zzone.domain.ChatConf;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 弹幕配置接口
 *
 * @author jinzhengze
 * @since 2023年10月15日
 */
@RestController
@RequestMapping("/conf")
public class ConfController {

    @RequestMapping(method = RequestMethod.GET)
    Object getData(@RequestParam(required = false) Integer id) {
        ChatConf data = ChatConf.mapper().getData(id);
        if (data == null)
            throw new RuntimeException("配置不存在");
        return Result.success(data);
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    Object queryData(ChatConf data) {
        return Result.success(ChatConf.mapper().likeData(data));
    }

    @RequestMapping(method = RequestMethod.POST)
    Object insertData(@RequestBody ChatConf data) {
        if (!StringUtils.hasText(data.getName()))
            throw new RuntimeException("配置名称不能为空");
        if (!StringUtils.hasText(data.getPlatform()))
            throw new RuntimeException("平台不能为空");
        if (!StringUtils.hasText(data.getRoom()))
            throw new RuntimeException("房间不能为空");
        if (ChatConf.mapper().insertData(data) < 1)
            throw new RuntimeException("配置新增失败");
        data.setId(null);
        return Result.success(ChatConf.mapper().listData(data).get(0).getId());
    }

    @RequestMapping(method = RequestMethod.PUT)
    Object updateData(@RequestBody ChatConf data) {
        if (data.getId() == null)
            throw new RuntimeException("配置对象ID不存在");
        if (!StringUtils.hasText(data.getName()))
            throw new RuntimeException("配置名称不能为空");
        if (!StringUtils.hasText(data.getPlatform()))
            throw new RuntimeException("平台不能为空");
        if (!StringUtils.hasText(data.getRoom()))
            throw new RuntimeException("房间不能为空");
        ChatConf find = ChatConf.mapper().getData(data.getId());
        if (find == null)
            throw new RuntimeException("配置对象数据为空");
        // 修改了平台信息/房间信息，若开启了监听，关闭所有连接的客户端
        boolean editFlag = !find.getPlatform().equals(data.getPlatform()) || !find.getRoom().equals(data.getRoom());
        if (editFlag && ChatWebsocketServer.CHANNEL_MAP.get(find.getKey()) != null)
            ChatWebsocketServer.CHANNEL_MAP.get(find.getKey()).close();
        Result<Integer> success = Result.success(ChatConf.mapper().updateData(data));
        ChatConf newData = ChatConf.mapper().getData(data.getId());
        JSONObject msg = new JSONObject();
        msg.put("cmd", "SYS_CONF");
        msg.put("data", newData);
        JSONArray arr = new JSONArray();
        arr.add(msg);
        if (ChatWebsocketServer.CHANNEL_MAP.get(find.getKey()) != null)
            ChatWebsocketServer.CHANNEL_MAP.get(find.getKey()).writeAndFlush(arr.toJSONString());
        return success;
    }

    @RequestMapping(method = RequestMethod.DELETE)
    Object deleteData(@RequestBody Integer[] ids) {
        return Result.success(ChatConf.mapper().deleteData(ids));
    }
}
